package com.breaking.second.activity.pager.collect;

import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.alibaba.fastjson2.JSONObject;
import com.breaking.second.R;
import com.breaking.second.activity.pager.BaseFragment;
import com.breaking.second.activity.pager.adapter.GoodsAdapter;
import com.breaking.second.databinding.PagerCollectBinding;
import com.breaking.second.entity.BookEntity;
import com.breaking.second.entity.GoodsEntity;
import com.breaking.second.entity.PageableEntity;
import com.breaking.second.recycler.RecyclerItemClickListener;
import com.breaking.second.utils.compat.DialogCompat;
import com.breaking.second.utils.compat.FormBodyCompat;
import com.breaking.second.utils.compat.RefreshLayoutCompat;
import com.breaking.second.utils.compat.SkeltonCompat;
import com.breaking.second.utils.okhttp.OkhttpUtil;
import com.ethanhua.skeleton.ViewSkeletonScreen;
import com.scwang.smart.refresh.layout.api.RefreshLayout;

import java.util.ArrayList;
import java.util.List;

import okhttp3.FormBody;

//已经发布的列表
public class CollectPager extends BaseFragment<PagerCollectBinding> {
    private final List<GoodsEntity> goodsEntities = new ArrayList<>();
    private GoodsAdapter adapter;
    private ViewSkeletonScreen d;

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initSmart();
        initRecycler();
        initData();
        initSkelton();
    }

    private void initSkelton() {
        d = SkeltonCompat.bind(binding.smart, R.layout.layout_empty).show();
    }

    private void initRecycler() {
        adapter = new GoodsAdapter(requireContext(), goodsEntities);
        binding.recycler.setLayoutManager(new LinearLayoutManager(requireContext()));
        binding.recycler.setAdapter(adapter);
        binding.recycler.addOnItemTouchListener(new RecyclerItemClickListener(requireContext(), this::onItemClick));
    }

    public void onItemClick(View view, int position) {
        GoodsEntity goodsEntity = goodsEntities.get(position);
        AlertDialog.Builder builder = new AlertDialog.Builder(requireContext());
        builder.setTitle("是否正式发布?");
        builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                sendGoods(goodsEntity);
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        });
        AlertDialog alertDialog = builder.show();
        DialogCompat.init(alertDialog.getWindow());
    }

    public void sendGoods(GoodsEntity goodsEntity) {

        JSONObject form = new JSONObject();
        form.put("addr", goodsEntity.getAddr());
        form.put("content", goodsEntity.getContent());
        form.put("imageCode", goodsEntity.getImageCode());
        form.put("price", goodsEntity.getPrice());
        form.put("typeId", goodsEntity.getTypeId());
        form.put("id", goodsEntity.getId());
        form.put("typeName", goodsEntity.getTypeName());
        form.put("userId", goodsEntity.tuserId);

        OkhttpUtil.Post("goods/change", form, new OkhttpUtil.CallbackListener(this::sendGoods));
    }

    private void sendGoods(BookEntity bookEntity) {
        if (bookEntity.isCode()) {
            getGoodsData();
        }
        Toast.makeText(requireActivity(), bookEntity.getMsg(), Toast.LENGTH_SHORT).show();
    }

    private void initSmart() {
        RefreshLayoutCompat.setRefresh(pageableVo, binding.smart, this::onRefresh);
    }

    private void onRefresh(RefreshLayout refreshLayout) {
        getGoodsData();
    }

    private void initData() {
        getGoodsData();
    }

    private void getGoodsData() {
        FormBody.Builder form = FormBodyCompat.to(pageableVo);
        OkhttpUtil.Get("goods/save", form, new OkhttpUtil.CallbackListener(this::getGoodsData));
    }

    private void getGoodsData(BookEntity bookEntity) {
        if (bookEntity.isCode()) {
            PageableEntity pageable = bookEntity.to(PageableEntity.class);

            if (pageable == null) return;
            if (pageable.getSize() != pageable.getSize()) {//禁止加载更多
                binding.smart.finishLoadMoreWithNoMoreData();
            }

            List<GoodsEntity> goodsEntities = pageable.toList(GoodsEntity.class);
            if (goodsEntities == null) return;
            if (goodsEntities.isEmpty()) return;
            if (pageable.getCurrent() == 0) {
                adapter.notifyItemRangeRemoved(0, this.goodsEntities.size() + 1);
                this.goodsEntities.clear();
            }

            int start = this.goodsEntities.size();
            this.goodsEntities.addAll(goodsEntities);
            adapter.notifyItemRangeChanged(start, this.goodsEntities.size());

            if (!this.goodsEntities.isEmpty()) {
                d.hide();
            }
        }
    }
}
